import Vue from 'vue'
import VueRouter from 'vue-router'
import JobHunterLogin from '../components/JobHunterLogin.vue'
import JobHunterHome from '../components/JobHunterHome.vue'
import JobDetail from '../components/JobDetail.vue'
import CompanyDetail from '../components/CompanyDetail.vue'
import JobHunterProfile from '../components/JobHunterProfile.vue'
import CompanyLogin from '../components/CompanyLogin.vue'
import '../assets/css/global.css'
import CompanyHome from "@/components/CompanyHome";
import JobHunterDetail from "@/components/JobHunterDetail";


Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: 'login' },
  { path: '/jobhunter/login', component: JobHunterLogin },
  { path: '/company/login', component: CompanyLogin },
  { path: '/jobhunter/home', component: JobHunterHome },
  { path: '/company/home', component: CompanyHome },
  { path: '/companys/:id', component: CompanyDetail },
  { path: '/jobhunter/profile', component: JobHunterProfile },
  { path: '/jobs/:id', component: JobDetail },
  { path: '/jobhunter/:id', component: JobHunterDetail }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫

router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径来
  // next 是一个函数，表示放行

  if(to.path == '/jobhunter/login') return next();
  if(to.path == '/company/login') return next();
  // 获取 token
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/jobhunter/login')
  next()
})

export default router
